package zookeeper;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;

public class ZooKeeperClient {

    private static String connectString = "120.55.54.181:2181";
    private static int sessionTimeout = 2000;
    private static ZooKeeper zkClient = null;

    public static void main(String[] args) {
        try {
            init();
            Thread.sleep(60 * 1000L);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void init() throws Exception {
        zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("WatchedEvent ....." + event.getState());
                if (event.getState() == KeeperState.Disconnected) {

                    System.out.println("Disconnected .....");
                }
                try {
                    zkClient.getChildren("/", true);
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        zkClient.create("/djf3", "djf".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

    }
}